import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

// 引入elementui 
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.css'

// 引入restful风格的封装请求
import {postRequest} from './utils/api'
import {putRequest} from './utils/api'
import {getRequest} from './utils/api'
import {deleteRequest} from './utils/api'
import {initMenu} from './utils/menus'
import { downloadRequest } from './utils/download'

// 关闭浏览器关于生产环境的提示
Vue.config.productionTip = false
Vue.use(ElementUI,{size:'small'})

// 插件形式使用请求
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequest = downloadRequest

router.beforeEach((to, from, next)=>{
  // 能拿到token的时候再进行菜单的获取
  if (window.sessionStorage.getItem('tokenStr')){
    initMenu(router,store);
    // 判断用户信息是否存在
    if(!window.sessionStorage.getItem('user')){
      return getRequest('/admin/info').then(resp =>{
        if(resp){
          // 存入用户信息 sessionStorage只能存储字符串  ，所以需要转换成字符串
          window.sessionStorage.setItem('user',JSON.stringify(resp));
		  store.commit('INIT_ADMIN',resp)
		  next();
        }
      })
    }
	next();
  }else{
    if(to.path == '/'){
      next();
    }else {
      next('/?redirect=' + to.path)
    }
  } 
  
})

new Vue({
  router,
  store,
  // 渲染
  render: h => h(App)
}).$mount('#app')  // 手动挂载app
